﻿#region [===== Using =====]
using System;
using System.Globalization;
#endregion

namespace TPG.SharePoint.Workflow
{
    /// <summary>
    /// A special type of <see cref="WorkflowControlPlaceHolder" /> for 
    /// providing a placeholder for <see cref="WorkflowDataControl" /> controls.
    /// </summary>
    /// <typeparam name="TData">The <see cref="System.Type"/> of the 
    /// data class.</typeparam>
    public class WorkflowDataControlPlaceHolder<TData>
        : WorkflowControlPlaceHolder
    {
        #region [===== Public methods =====]
        /// <summary>
        /// Gets the <see cref="WorkflowDataControl" /> hosted in this 
        /// placeholder.
        /// </summary>
        /// <returns>The data control.</returns>
        public WorkflowDataControl<TData> GetWorkflowControl()
        {
            return (WorkflowDataControl<TData>)Control;
        }
        #endregion

        #region [===== Protected methods =====]
        /// <summary>
        /// Initializes the placeholder and verifies if the data control 
        /// provides the right type of data.
        /// </summary>
        /// <param name="e">An EventArgs containing event data.</param>
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            IWorkflowDataPage<TData> page = Page as IWorkflowDataPage<TData>;
            if (page == null)
            {
                throw new WorkflowException(
                    string.Format(CultureInfo.CurrentCulture, "This placeholder control must be placed on a page implementing IWorkflowDataPage<{0}>.",
                        typeof(TData).AssemblyQualifiedName));
            }
            WorkflowDataControl<TData> control = Control as WorkflowDataControl<TData>;
            if (control == null)
            {
                throw new WorkflowException(
                    string.Format(CultureInfo.CurrentCulture, "The placeholder control can only host controls deriving from WorkflowDataControl<{0}>.",
                        typeof(TData).AssemblyQualifiedName));
            }
            string serializationModeString = LookupMetadata(
                page.WorkflowTemplate, page.PageType,
                "SerializationMode", true);
            if (String.IsNullOrEmpty(serializationModeString))
            {
                serializationModeString = LookupMetadata(
                    page.WorkflowTemplate, page.PageType,
                    "SerializationMode", false);
            }
            if (String.IsNullOrEmpty(serializationModeString) == false)
            {
                if (Enum.IsDefined(typeof(SerializationMode), serializationModeString) == false)
                {
                    throw new WorkflowException(
                        String.Format("SerializationMode {0} for control location {1} not defined",
                        serializationModeString, Location));
                }
                SerializationMode serializationMode =
                     (SerializationMode)Enum.Parse(typeof(SerializationMode), serializationModeString);
                control.SerializationMode = serializationMode;
            }
        }
        #endregion
    }
}
